
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>数据结构高级内容 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="函数.html" />
    
    
    <link rel="prev" href="数据结构.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    前言
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="基础.html">
            
                <a href="基础.html">
            
                    
                    基础
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="字符串处理.html">
            
                <a href="字符串处理.html">
            
                    
                    字符串处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="数据结构.html">
            
                <a href="数据结构.html">
            
                    
                    数据结构
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.5" data-path="数据结构高级内容.html">
            
                <a href="数据结构高级内容.html">
            
                    
                    数据结构高级内容
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="函数.html">
            
                <a href="函数.html">
            
                    
                    函数
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="函数式编程.html">
            
                <a href="函数式编程.html">
            
                    
                    函数式编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="面向对象编程.html">
            
                <a href="面向对象编程.html">
            
                    
                    面向对象编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="类的特殊方法.html">
            
                <a href="类的特殊方法.html">
            
                    
                    类的特殊方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="异常处理.html">
            
                <a href="异常处理.html">
            
                    
                    异常处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="IO编程.html">
            
                <a href="IO编程.html">
            
                    
                    IO编程
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="模块.html">
            
                <a href="模块.html">
            
                    
                    模块
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="标准库.html">
            
                <a href="标准库.html">
            
                    
                    标准库
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="测试.html">
            
                <a href="测试.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.15" data-path="调试.html">
            
                <a href="调试.html">
            
                    
                    调试
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >数据结构高级内容</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6570;&#x636E;&#x7ED3;&#x6784;&#x9AD8;&#x7EA7;&#x5185;&#x5BB9;">&#x6570;&#x636E;&#x7ED3;&#x6784;&#x9AD8;&#x7EA7;&#x5185;&#x5BB9;</h1>
<hr>
<h2 id="&#x5207;&#x7247;">&#x5207;&#x7247;</h2>
<p>&#x53EF;&#x4EE5;&#x5207;&#x7247;&#x7684;&#x6570;&#x636E;&#x7C7B;&#x578B;&#xFF1A;list&#x3001;tuple&#x3001;str</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>[<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>][<span class="hljs-number">0</span>:<span class="hljs-number">3</span>]
(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span>(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>)[<span class="hljs-number">0</span>:<span class="hljs-number">3</span>]
(<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>)
<span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-string">&apos;ABCDEFG&apos;</span>[<span class="hljs-number">0</span>:<span class="hljs-number">3</span>]
<span class="hljs-string">&apos;ABC&apos;</span>
</code></pre>
<h2 id="&#x8FED;&#x4EE3;">&#x8FED;&#x4EE3;</h2>
<ul>
<li><strong>Iterator&#x7C7B;&#x578B;</strong>&#xFF1A;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x60F0;&#x6027;&#x5E8F;&#x5217;&#xFF0C;Iterator &#x7684;&#x8BA1;&#x7B97;&#x662F;&#x60F0;&#x6027;&#x7684;&#xFF0C;&#x53EA;&#x6709;&#x5728;&#x9700;&#x8981;&#x8FD4;&#x56DE;&#x4E0B;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x65F6;&#x5B83;&#x624D;&#x4F1A;&#x8BA1;&#x7B97;&#xFF0C;&#x4E14;&#x4E00;&#x6B21;&#x8BA1;&#x7B97;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;&#x6570;&#x636E;&#x3002;&#x5177;&#x6709; <code>__next__()</code> &#x65B9;&#x6CD5;&#x3002;Iterator &#x751A;&#x81F3;&#x53EF;&#x4EE5;&#x8868;&#x793A;&#x4E00;&#x4E2A;&#x65E0;&#x9650;&#x5927;&#x7684;&#x6570;&#x636E;&#x6D41;&#xFF0C;&#x4F8B;&#x5982;&#x5168;&#x4F53;&#x81EA;&#x7136;&#x6570;&#x3002;    </li>
<li><strong>Iterable&#x7C7B;&#x578B;</strong>&#xFF1A;&#x4ECE;&#x5B83;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x6216;&#x95F4;&#x63A5;&#x5F97;&#x5230;&#x4E00;&#x4E2A;Iterator&#x7684;&#x5BF9;&#x8C61;&#x3002;&#x672C;&#x8EAB;&#x662F;Iterator&#x5BF9;&#x8C61;&#xFF0C;&#x6216;&#x80FD;&#x901A;&#x8FC7;<code>iter()</code>&#x83B7;&#x5F97;&#x5230;&#x4E00;&#x4E2A;Iterator&#x5BF9;&#x8C61;&#x3002;  </li>
</ul>
<p>generator&#x662F; Iterator &#x5BF9;&#x8C61;&#x3002;&#x751F;&#x6210;&#x5668;&#x4E0D;&#x4F46;&#x53EF;&#x4EE5;&#x4F5C;&#x7528;&#x4E8E; for &#x5FAA;&#x73AF;&#xFF0C;&#x8FD8;&#x53EF;&#x4F5C;&#x7528;&#x4E8E; next()&#x51FD;&#x6570;&#x3002;
&#x96C6;&#x5408;&#x6570;&#x636E;&#x7C7B;&#x578B;&#x5982; list&#x3001; dict&#x3001; str &#x7B49;&#x662F; Iterable &#x4F46;&#x4E0D;&#x662F; Iterator&#xFF0C;&#x4E0D;&#x8FC7;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; iter()&#x51FD;&#x6570;&#x5C06;&#x5176;&#x8F6C;&#x6362;&#x4E3A; Iterator &#x5BF9;&#x8C61;&#x3002;&#x4F7F;&#x7528;list&#x7B49;&#x662F;&#x6C38;&#x8FDC;&#x4E0D;&#x53EF;&#x80FD;&#x5B58;&#x50A8;&#x5168;&#x4F53;&#x81EA;&#x7136;&#x6570;&#x7684;&#x3002;</p>
<blockquote>
<p>&#x51E1;&#x662F;&#x53EF;&#x4F5C;&#x7528;&#x4E8E; for &#x5FAA;&#x73AF;&#x7684;&#x5BF9;&#x8C61;&#x90FD;&#x662F; Iterable &#x7C7B;&#x578B;&#xFF08;&#x4E5F;&#x79F0;&#x4E4B;&#x4E3A;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#xFF09;&#xFF1B;
&#x51E1;&#x662F;&#x53EF;&#x4F5C;&#x7528;&#x4E8E; next()&#x51FD;&#x6570;&#x7684;&#x5BF9;&#x8C61;&#x90FD;&#x662F; Iterator &#x7C7B;&#x578B;&#xFF08;&#x4E5F;&#x79F0;&#x4E4B;&#x4E3A;&#x8FED;&#x4EE3;&#x5668;&#xFF09;&#xFF1B;</p>
</blockquote>
<h3 id="iterator">Iterator</h3>
<p>&#x4F7F;&#x7528; isinstance()&#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x662F; Iterator &#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> Iterator
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance((x <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>)), Iterator)
<span class="hljs-keyword">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance([], Iterator)
<span class="hljs-keyword">False</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance({}, Iterator)
<span class="hljs-keyword">False</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(<span class="hljs-string">&apos;abc&apos;</span>, Iterator)
<span class="hljs-keyword">False</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(iter([]), Iterator)
<span class="hljs-keyword">True</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(iter(<span class="hljs-string">&apos;abc&apos;</span>), Iterator)
<span class="hljs-keyword">True</span>
</code></pre>
<h3 id="iterable">Iterable</h3>
<p>&#x901A;&#x8FC7;isinstance&#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x5224;&#x65AD;&#x4E00;&#x4E2A;&#x5BF9;&#x8C61;&#x662F;&#x5426;&#x662F;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">from</span> collections <span class="hljs-keyword">import</span> Iterable
<span class="hljs-meta">&gt;&gt;&gt; </span>isinstance(<span class="hljs-string">&apos;abc&apos;</span>, Iterable) <span class="hljs-comment"># str &#x662F;&#x5426;&#x53EF;&#x8FED;&#x4EE3;</span>
<span class="hljs-keyword">True</span>
</code></pre>
<p>&#x5728; Python &#x4E2D;&#xFF0C;&#x8FED;&#x4EE3;&#x662F;&#x901A;&#x8FC7; <code>for ... in</code> &#x6765;&#x5B8C;&#x6210;&#x7684;&#x3002;for &#x5FAA;&#x73AF;&#x672C;&#x8D28;&#x4E0A;&#x5C31;&#x662F;&#x901A;&#x8FC7;&#x4E0D;&#x65AD;&#x8C03;&#x7528; <code>next()</code>&#x51FD;&#x6570;&#x5B9E;&#x73B0;&#x7684;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>]:
    <span class="hljs-keyword">pass</span>
</code></pre>
<p>&#x5B9E;&#x9645;&#x4E0A;&#x5B8C;&#x5168;&#x7B49;&#x4EF7;&#x4E8E;&#xFF1A;  </p>
<pre><code class="lang-python">it = iter([<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>])     <span class="hljs-comment"># &#x9996;&#x5148;&#x83B7;&#x5F97; Iterator &#x5BF9;&#x8C61;</span>
<span class="hljs-keyword">while</span> <span class="hljs-keyword">True</span>:                    <span class="hljs-comment"># &#x5FAA;&#x73AF;</span>
    <span class="hljs-keyword">try</span>:
        x = next(it)           <span class="hljs-comment"># &#x83B7;&#x5F97;&#x4E0B;&#x4E00;&#x4E2A;&#x503C;</span>
    <span class="hljs-keyword">except</span> StopIteration:
        <span class="hljs-keyword">break</span>                  <span class="hljs-comment"># &#x9047;&#x5230; StopIteration &#x5C31;&#x9000;&#x51FA;&#x5FAA;&#x73AF;</span>
</code></pre>
<p>list&#x6216; tuple &#x7684;&#x8FED;&#x4EE3;&#x662F;&#x901A;&#x8FC7;&#x4E0B;&#x6807;&#x5B8C;&#x6210;&#x7684;&#x3002;<br>dict &#x9ED8;&#x8BA4;&#x60C5;&#x51B5;&#x4E0B;&#x8FED;&#x4EE3;&#x7684;&#x662F; key&#x3002;&#x5982;&#x679C;&#x8981;&#x8FED;&#x4EE3; value&#xFF0C;&#x53EF;&#x4EE5;&#x7528; <code>for value in d.values()</code>&#xFF0C;&#x5982;&#x679C;&#x8981;&#x540C;&#x65F6;&#x8FED;&#x4EE3; key &#x548C; value&#xFF0C;&#x53EF;&#x4EE5;&#x7528; <code>for k, v in d.items()</code><br>&#x5B57;&#x7B26;&#x4E32;&#x7684;&#x8FED;&#x4EE3;&#xFF0C;&#x4F8B;&#x5982;&#xFF1A;<code>for ch in &apos;ABC&apos;</code>  </p>
<p>&#x901A;&#x8FC7;enumerate &#x51FD;&#x6570;&#x53EF;&#x4EE5;&#x628A;&#x4E00;&#x4E2A; list &#x53D8;&#x6210;&#x7D22;&#x5F15;-&#x5143;&#x7D20;&#x5BF9;&#xFF0C;&#x8FD9;&#x6837;&#x5C31;&#x53EF;&#x4EE5;&#x5728; for &#x5FAA;&#x73AF;&#x4E2D;&#x540C;&#x65F6;&#x8FED;&#x4EE3;&#x7D22;&#x5F15;&#x548C;&#x5143;&#x7D20;&#x672C;&#x8EAB;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span><span class="hljs-keyword">for</span> i, value <span class="hljs-keyword">in</span> enumerate([<span class="hljs-string">&apos;A&apos;</span>, <span class="hljs-string">&apos;B&apos;</span>, <span class="hljs-string">&apos;C&apos;</span>]):
<span class="hljs-meta">... </span>print(i, value)
...
<span class="hljs-number">0</span> A
<span class="hljs-number">1</span> B
<span class="hljs-number">2</span> C
</code></pre>
<h2 id="&#x63A8;&#x5BFC;&#x5F0F;&#xFF08;comprehension&#xFF09;">&#x63A8;&#x5BFC;&#x5F0F;&#xFF08;Comprehension&#xFF09;</h2>
<p>&#x63A8;&#x5BFC;&#x5F0F;&#x662F;&#x4ECE;&#x4E00;&#x4E2A;&#x6216;&#x591A;&#x4E2A;&#x8FED;&#x4EE3;&#x5668;&#x7B80;&#x6D01;&#x5FEB;&#x901F;&#x521B;&#x5EFA;&#x6570;&#x636E;&#x7ED3;&#x6784;&#x7684;&#x4E00;&#x79CD;&#x65B9;&#x6CD5;&#x3002;<br>&#x4EE5;&#x5217;&#x8868;&#x63A8;&#x5BFC;&#x5F0F;&#x6765;&#x5206;&#x6790;&#x4E0B;&#x539F;&#x7406;&#xFF1A;<br><code>[(x,y) for x in range(3) if x != 0 for y in range(5) if y != 0]</code><br>&#x7B49;&#x4EF7;&#x4E8E;&#xFF1A;  </p>
<pre><code class="lang-python">result = []
<span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">3</span>):
    <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> x != <span class="hljs-number">0</span>:
        <span class="hljs-keyword">continue</span>
    <span class="hljs-keyword">for</span> y <span class="hljs-keyword">in</span> range(<span class="hljs-number">5</span>):
        <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> y != <span class="hljs-number">0</span>:
            <span class="hljs-keyword">continue</span>
        result.append((x,y))
</code></pre>
<blockquote>
<p>[&#x6CE8;] &#x5982;&#x679C;&#x63A8;&#x5BFC;&#x5F0F;&#x4E2D;&#x6709;&#x591A;&#x6761;for&#x5B50;&#x53E5;&#x6216;if&#x5B50;&#x53E5;&#xFF0C;&#x5D4C;&#x5957;&#x6B21;&#x5E8F;&#x5C31;&#x662F;&#x5B83;&#x4EEC;&#x7684;&#x4E66;&#x5199;&#x987A;&#x5E8F;&#x3002;</p>
</blockquote>
<h3 id="&#x5217;&#x8868;&#x63A8;&#x5BFC;&#x5F0F;">&#x5217;&#x8868;&#x63A8;&#x5BFC;&#x5F0F;</h3>
<p>&#x57FA;&#x672C;&#x683C;&#x5F0F;&#xFF1A;<br><code>[expression for item in iterable if condition]</code><br>&#x4E3E;&#x4F8B;&#xFF1A;  </p>
<pre><code class="lang-python">mylist = [x <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">6</span>)]
mylist = [(x, y) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>] <span class="hljs-keyword">for</span> y <span class="hljs-keyword">in</span> [<span class="hljs-number">1</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>]]
mylist = [(x, y) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>] <span class="hljs-keyword">for</span> y <span class="hljs-keyword">in</span> [<span class="hljs-number">1</span>,<span class="hljs-number">5</span>,<span class="hljs-number">6</span>] <span class="hljs-keyword">if</span> x != y]
mylist = [(x, y) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>] <span class="hljs-keyword">if</span> x != <span class="hljs-number">1</span> <span class="hljs-keyword">for</span> y <span class="hljs-keyword">in</span> <span class="hljs-string">&apos;abc&apos;</span> <span class="hljs-keyword">if</span> y != <span class="hljs-string">&apos;a&apos;</span>]
</code></pre>
<h3 id="&#x5B57;&#x5178;&#x63A8;&#x5BFC;&#x5F0F;">&#x5B57;&#x5178;&#x63A8;&#x5BFC;&#x5F0F;</h3>
<p>&#x57FA;&#x672C;&#x683C;&#x5F0F;&#xFF1A;<br><code>{key_expression:value_expression for item in iterable if condition}</code><br>&#x4E3E;&#x4F8B;&#xFF1A;  </p>
<pre><code class="lang-python">text = <span class="hljs-string">&apos;hello erick&apos;</span>
mydict = {x:text.count(x) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> set(text) <span class="hljs-keyword">if</span> x != <span class="hljs-string">&apos; &apos;</span>}  
<span class="hljs-comment">#text&#x5B58;&#x5728;&#x91CD;&#x590D;&#x5B57;&#x6BCD;&#xFF0C;&#x6D6A;&#x8D39;&#x65F6;&#x95F4;&#xFF0C;&#x56E0;&#x6B64;&#x7528;set&#x6765;&#x6539;&#x5584;</span>
</code></pre>
<h3 id="&#x96C6;&#x5408;&#x63A8;&#x5BFC;&#x5F0F;">&#x96C6;&#x5408;&#x63A8;&#x5BFC;&#x5F0F;</h3>
<p>&#x4E0E;&#x5217;&#x8868;&#x63A8;&#x5BFC;&#x5F0F;&#x76F8;&#x4F3C;&#xFF0C;&#x5C06;[]&#x6539;&#x6210;{}&#x5373;&#x53EF;&#x3002;<br>&#x4E3E;&#x4F8B;&#xFF1A;  </p>
<pre><code class="lang-python">myset = {x <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> <span class="hljs-string">&apos;abc&apos;</span>}
</code></pre>
<h3 id="&#x751F;&#x6210;&#x5668;&#x63A8;&#x5BFC;&#x5F0F;&#xFF08;&#x751F;&#x6210;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;generator-expressions&#xFF09;">&#x751F;&#x6210;&#x5668;&#x63A8;&#x5BFC;&#x5F0F;&#xFF08;&#x751F;&#x6210;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;Generator Expressions&#xFF09;</h3>
<p>&#x5143;&#x7EC4;&#x6CA1;&#x6709;&#x63A8;&#x5BFC;&#x5F0F;&#xFF0C;&#x5B83;&#x7684;&#x5706;&#x62EC;&#x53F7;&#x662F;&#x7528;&#x6765;&#x505A;&#x751F;&#x6210;&#x5668;&#x63A8;&#x5BFC;&#x5F0F;&#x7684;&#x3002;<br>&#x6700;&#x7B80;&#x5355;&#x7684;&#x683C;&#x5F0F;&#xFF1A;<br><code>(expression for item in iterable if condition)</code><br>&#x4E3E;&#x4F8B;&#xFF1A;  </p>
<pre><code class="lang-python">mygenerator = (x <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">8</span>) <span class="hljs-keyword">if</span> x != <span class="hljs-number">0</span>) <span class="hljs-comment">#&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; generator &#x5BF9;&#x8C61;</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> mygenerator:
    print(i)
</code></pre>
<p>&#x5982;&#x679C;&#x751F;&#x6210;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#x4F5C;&#x4E3A;&#x51FD;&#x6570;&#x552F;&#x4E00;&#x53C2;&#x6570;&#x53BB;&#x8C03;&#x7528;&#x65F6;&#xFF0C;&#x5B83;&#x7684;&#x5706;&#x62EC;&#x53F7;&#x53EF;&#x4EE5;&#x7701;&#x7565;&#x3002;<br>&#x4E3E;&#x4F8B;&#xFF1A;  </p>
<pre><code class="lang-python">result = sum(x*y <span class="hljs-keyword">for</span> x,y <span class="hljs-keyword">in</span> zip([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>], [<span class="hljs-number">3</span>,<span class="hljs-number">4</span>]))
mytuple = tuple(x <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>))
mylist  = list(x <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>))
mydict  = dict( (str(x), x) <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> range(<span class="hljs-number">10</span>) )
</code></pre>
<h2 id="&#x5E8F;&#x5217;&#x89E3;&#x5305;&#xFF08;sequence-unpacking&#xFF09;">&#x5E8F;&#x5217;&#x89E3;&#x5305;&#xFF08;sequence unpacking&#xFF09;</h2>
<p>&#x5E8F;&#x5217;&#x89E3;&#x5305;&#x7684;&#x672C;&#x8D28;&#x662F;&#x628A;&#x4E00;&#x4E2A;&#x5E8F;&#x5217;&#x6216;&#x4EFB;&#x4F55;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#x4E2D;&#x7684;&#x5143;&#x7D20;&#x540C;&#x65F6;&#x8D4B;&#x503C;&#x7ED9;&#x591A;&#x4E2A;&#x53D8;&#x91CF;&#x3002;<br>&#x5982;&#x679C;&#x7B49;&#x53F7;&#x53F3;&#x4FA7;&#x542B;&#x6709;&#x8868;&#x8FBE;&#x5F0F;&#xFF0C;&#x4F1A;&#x628A;&#x6240;&#x6709;&#x8868;&#x8FBE;&#x5F0F;&#x7684;&#x503C;&#x5148;&#x8BA1;&#x7B97;&#x51FA;&#x6765;&#xFF0C;&#x7136;&#x540E;&#x518D;&#x8FDB;&#x884C;&#x8D4B;&#x503C;&#x3002;<br>&#x4F8B;&#x5982;&#xFF1A;  </p>
<ul>
<li>&#x591A;&#x4E2A;&#x53D8;&#x91CF;&#x540C;&#x65F6;&#x8D4B;&#x503C;<pre><code class="lang-python">x, y, z = <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>
v_tuple = (<span class="hljs-keyword">False</span>, <span class="hljs-number">2</span>, <span class="hljs-string">&apos;hello&apos;</span>)
(a, b, c) = v_tuple
a, b, c = v_tuple
</code></pre>
</li>
<li>&#x4EA4;&#x6362;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x7684;&#x503C;<pre><code class="lang-python">a, b = b, a  <span class="hljs-comment">#&#x4EA4;&#x6362;&#x4E24;&#x4E2A;&#x53D8;&#x91CF;&#x7684;&#x6700;&#x5FEB;&#x7684;&#x65B9;&#x5F0F;</span>
</code></pre>
</li>
<li>&#x5B57;&#x7B26;&#x4E32;&#x8FDB;&#x884C;&#x89E3;&#x5305;<pre><code class="lang-python">x, y, z = <span class="hljs-string">&apos;abc&apos;</span>
</code></pre>
</li>
<li>&#x89E3;&#x5305;&#x7ED9;&#x5207;&#x7247;&#x8D4B;&#x503C;<pre><code class="lang-python">x = [<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>]
x[:<span class="hljs-number">2</span>] = <span class="hljs-string">&apos;0123&apos;</span> <span class="hljs-comment">#&#x7ED3;&#x679C;x&#x4E3A;&#xFF1A;[&apos;0&apos;, &apos;1&apos;, &apos;2&apos;, &apos;3&apos;, 3]</span>
</code></pre>
</li>
<li>&#x8FED;&#x4EE3;&#x5668;&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x89E3;&#x5305;<pre><code class="lang-python">x, y, z = iter([<span class="hljs-number">1</span>,<span class="hljs-number">2</span>,<span class="hljs-number">3</span>])
</code></pre>
</li>
<li>range&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x89E3;&#x5305;<pre><code class="lang-python">x, y, z = range(<span class="hljs-number">3</span>)
</code></pre>
</li>
<li>&#x53EF;&#x8FED;&#x4EE3;&#x7684;map&#x5BF9;&#x8C61;&#x8FDB;&#x884C;&#x89E3;&#x5305;<pre><code class="lang-python">x, y, z = map(str, range(<span class="hljs-number">3</span>))
</code></pre>
</li>
<li>sorted&#x51FD;&#x6570;&#x8FD4;&#x56DE;&#x6392;&#x5E8F;&#x540E;&#x7684;&#x5217;&#x8868;&#x8FDB;&#x884C;&#x89E3;&#x5305;<pre><code class="lang-python">x, y, z = sorted([<span class="hljs-number">1</span>,<span class="hljs-number">3</span>,<span class="hljs-number">2</span>])
</code></pre>
</li>
<li>&#x5982;&#x679C;&#x60F3;&#x5C06;&#x7ED3;&#x679C;&#x89E3;&#x91CA;&#x4E3A; <code>(a, &lt;everything else&gt;)</code>&#xFF0C;&#x4F60;&#x9700;&#x8981;&#x7528;&#x661F;&#x6765;&#x8868;&#x793A;&#xFF0C;&#x5C31;&#x5982;&#x5728;&#x51FD;&#x6570;&#x53C2;&#x6570;&#x4E2D;&#x90A3;&#x6837;&#xFF0C;&#x4E0D;&#x540C;&#x7684;&#x662F;&#x8FD9;&#x91CC;&#x89E3;&#x538B;&#x540E;<code>*</code>&#x540E;&#x9762;&#x7684;&#x53D8;&#x91CF;&#x662F;list&#x7C7B;&#x578B;&#xFF08;&#x5982;&#x679C;&#x6CA1;&#x6709;&#x5143;&#x7D20;&#x5219;&#x4E3A;&#x7A7A;&#x5217;&#x8868;&#xFF09;&#x3002;<pre><code class="lang-python">a, *b = [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>]   <span class="hljs-comment">#a&#x4E3A;1  b&#x4E3A;[2,3,4]</span>
a, *b = &#xFF08;<span class="hljs-number">1</span>&#xFF0C;&#xFF09;        <span class="hljs-comment">#a&#x4E3A;1  b&#x4E3A;[]</span>
</code></pre>
</li>
</ul>
<h2 id="&#x751F;&#x6210;&#x5668;">&#x751F;&#x6210;&#x5668;</h2>
<blockquote>
<p>&#x6CE8;&#x610F;&#xFF1A;&#x4E00;&#x4E2A;&#x751F;&#x6210;&#x5668;&#x5BF9;&#x8C61;&#x53EA;&#x80FD;&#x904D;&#x5386;&#x4E00;&#x6B21;&#xFF0C;&#x4E0D;&#x80FD;&#x53CD;&#x590D;&#x904D;&#x5386;&#x3002;&#x5982;&#x679C;&#x9700;&#x8981;&#x91CD;&#x65B0;&#x904D;&#x5386;&#xFF0C;&#x9700;&#x8981;&#x751F;&#x6210;&#x4E00;&#x4E2A;&#x65B0;&#x7684;&#x751F;&#x6210;&#x5668;&#x5BF9;&#x8C61;&#x3002;  </p>
</blockquote>
<p>&#x521B;&#x5EFA;generator&#x5BF9;&#x8C61;&#x7684;&#x65B9;&#x6CD5;&#xFF1A;</p>
<ul>
<li>&#x4F7F;&#x7528;&#x751F;&#x6210;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;&#x751F;&#x6210;&#x5668;&#x63A8;&#x5BFC;&#x5F0F;&#xFF09;</li>
<li>&#x4F7F;&#x7528;&#x751F;&#x6210;&#x5668;&#x51FD;&#x6570;</li>
</ul>
<h3 id="&#x751F;&#x6210;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;&#x751F;&#x6210;&#x5668;&#x63A8;&#x5BFC;&#x5F0F;&#xFF09;">&#x751F;&#x6210;&#x5668;&#x8868;&#x8FBE;&#x5F0F;&#xFF08;&#x751F;&#x6210;&#x5668;&#x63A8;&#x5BFC;&#x5F0F;&#xFF09;</h3>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>f = (x <span class="hljs-keyword">for</span> x <span class="hljs-keyword">in</span> &#x2018;abc&#x2019;) <span class="hljs-comment">#&#x8FD4;&#x56DE;&#x4E00;&#x4E2A; generator &#x5BF9;&#x8C61;</span>
<span class="hljs-meta">&gt;&gt;&gt; </span>f
&lt;generator object &lt;genexpr&gt; at <span class="hljs-number">0x1022ef630</span>&gt;
</code></pre>
<h3 id="&#x751F;&#x6210;&#x5668;&#x51FD;&#x6570;">&#x751F;&#x6210;&#x5668;&#x51FD;&#x6570;</h3>
<p>&#x4EFB;&#x4F55;&#x5305;&#x542B;<code>yield</code>&#x5173;&#x952E;&#x5B57;&#x7684;&#x51FD;&#x6570;&#x90FD;&#x662F;&#x4E00;&#x4E2A;&#x751F;&#x6210;&#x5668;&#x51FD;&#x6570;&#x3002;<br>1&#xFF09;&#x8C03;&#x7528;&#x751F;&#x6210;&#x5668;&#x51FD;&#x6570;&#x5C06;&#x8FD4;&#x56DE;&#x4E00;&#x4E2A;generator &#x5BF9;&#x8C61;&#xFF1B;<br>2&#xFF09;&#x7B2C;&#x4E00;&#x6B21;&#x5BF9;&#x751F;&#x6210;&#x5668;&#x4F7F;&#x7528;next&#x65B9;&#x6CD5;&#x65F6;&#xFF0C;&#x751F;&#x6210;&#x5668;&#x624D;&#x5F00;&#x59CB;&#x6267;&#x884C;&#x751F;&#x6210;&#x5668;&#x51FD;&#x6570;&#xFF1B;<br>3&#xFF09;&#x6BCF;&#x6B21;&#x9047;&#x5230;yield&#x5219;&#x4E2D;&#x65AD;&#x8FD4;&#x56DE;&#xFF0C;&#x5E76;&#x5C06; yield &#x540E;&#x7684;&#x53C2;&#x6570;&#x4F5C;&#x4E3A;&#x6B64;&#x6B21;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#xFF1B;<br>4&#xFF09;&#x518D;&#x6B21;&#x6267;&#x884C;&#x65F6;&#x4ECE;&#x4E0A;&#x6B21;&#x4E2D;&#x65AD;&#x7684; yield &#x8BED;&#x53E5;&#x5904;&#x7EE7;&#x7EED;&#x6267;&#x884C;&#xFF1B;<br>5&#xFF09;&#x5F53;&#x8C03;&#x7528; next &#x65B9;&#x6CD5;&#x65F6;&#x751F;&#x6210;&#x5668;&#x51FD;&#x6570;&#x7ED3;&#x675F;&#x6216;&#x9047;&#x5230;return&#x8BED;&#x53E5;&#xFF0C;&#x5219;&#x6B64;&#x6B21;&#x8C03;&#x7528;&#x5C06;&#x629B;&#x51FA; StopIteration &#x5F02;&#x5E38;&#x3002;<br>&#x4E3E;&#x4F8B;&#xFF1A;  </p>
<pre><code class="lang-python"><span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">genefunc</span><span class="hljs-params">()</span>:</span>
    <span class="hljs-string">&apos;&apos;&apos; &#x751F;&#x6210;&#x5668;&#x51FD;&#x6570; &apos;&apos;&apos;</span>
    <span class="hljs-keyword">yield</span> <span class="hljs-number">1</span>
    <span class="hljs-keyword">yield</span> <span class="hljs-string">&apos;hello&apos;</span>
    <span class="hljs-keyword">yield</span> (<span class="hljs-number">1</span>,<span class="hljs-number">2</span>)
g = genefunc()
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> g:
    print(i)
</code></pre>
<h3 id="generator&#x5BF9;&#x8C61;&#x7684;&#x4F7F;&#x7528;">generator&#x5BF9;&#x8C61;&#x7684;&#x4F7F;&#x7528;</h3>
<p>&#x53EF;&#x4EE5;&#x901A;&#x8FC7; next()&#x51FD;&#x6570;&#x83B7;&#x5F97; generator &#x7684;&#x4E0B;&#x4E00;&#x4E2A;&#x8FD4;&#x56DE;&#x503C;&#xFF0C;&#x76F4;&#x5230;&#x8BA1;&#x7B97;&#x5230;&#x6700;&#x540E;&#x4E00;&#x4E2A;&#x5143;&#x7D20;&#xFF0C;&#x6CA1;&#x6709;&#x66F4;&#x591A;&#x7684;&#x5143;&#x7D20;&#x65F6;&#xFF0C;&#x4F1A;&#x629B;&#x51FA;StopIteration &#x7684;&#x9519;&#x8BEF;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-meta">&gt;&gt;&gt; </span>next(f)
a
<span class="hljs-meta">&gt;&gt;&gt; </span>next(f)
b
<span class="hljs-meta">&gt;&gt;&gt; </span>next(f)
c
<span class="hljs-meta">&gt;&gt;&gt; </span>next(f)
Traceback (most recent call last):
File <span class="hljs-string">&quot;&lt;stdin&gt;&quot;</span>, line <span class="hljs-number">1</span>, <span class="hljs-keyword">in</span> &lt;module&gt;
StopIteration
</code></pre>
<p>&#x4E3A;&#x4E86;&#x7B80;&#x5316;&#xFF0C;&#x8FD8;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;for &#x5FAA;&#x73AF;&#xFF0C;&#x56E0;&#x4E3A; generator &#x4E5F;&#x662F;&#x53EF;&#x8FED;&#x4EE3;&#x5BF9;&#x8C61;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> f:
    print(i)
</code></pre>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="数据结构.html" class="navigation navigation-prev " aria-label="Previous page: 数据结构">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="函数.html" class="navigation navigation-next " aria-label="Next page: 函数">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"数据结构高级内容","level":"1.5","depth":1,"next":{"title":"函数","level":"1.6","depth":1,"path":"函数.md","ref":"函数.md","articles":[]},"previous":{"title":"数据结构","level":"1.4","depth":1,"path":"数据结构.md","ref":"数据结构.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":[],"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"数据结构高级内容.md","mtime":"2017-08-23T14:53:13.315Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2017-08-27T04:11:18.522Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

